/*
 *	This file is part of Lounge-Cast.
 *
 *  Author:  	Markus Moormann
 *	E-mail:  	mmoorman@uni-osnabrueck.de
 *	Copyright:	University of Osnabrueck (Zentrum Virtuos)
 * 	Website:	www.uni-osnabrueck.de
 *	Date:		16.02.2011
 *
 *	Lounge-Cast is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *	Lounge-Cast is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Lounge-Cast.  If not, see <http://www.gnu.org/licenses/>.
 */
package multimedia.control.command
{
	import com.adobe.cairngorm.commands.SequenceCommand;
	import com.adobe.cairngorm.control.CairngormEvent;
	
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	
	import multimedia.control.event.GetPreferencesEvent;
	import multimedia.control.event.UpdatePreferencesEvent;
	import multimedia.model.ModelLocator;
	import multimedia.util.LogUtil;
	
	import mx.logging.ILogger;
	
	/**
	 * 	GetPreferencesCommand
	 * 
	 *	@author jemden
	 *	@version 1.0
	 */
	public class GetPreferencesCommand extends SequenceCommand
	{
		private var model:ModelLocator;
		
		private var e:GetPreferencesEvent;
		
		private static const LOG:ILogger = LogUtil.getLogger(GetPreferencesCommand);
		
		override public function execute(event:CairngormEvent):void
		{
			LOG.debug("<<< Loading preferences >>>");
			this.model = ModelLocator.getInstance();
			this.e = event as GetPreferencesEvent;
			this.nextEvent = e.nextEvent;
			
			var prefsFile:File; // The preferences prefsFile
			prefsFile = File.applicationStorageDirectory;
			prefsFile = prefsFile.resolvePath(ModelLocator.XML_FILE_NAME); 
			
			
			
			if (prefsFile.exists) {
				
				
				var stream:FileStream = new FileStream();
    			stream.open(prefsFile, FileMode.READ);
    			var prefsXML:XML = XML(stream.readUTFBytes(stream.bytesAvailable));
				stream.close();
				
				model.url = prefsXML.webservice.@url;
				model.apiKey = prefsXML.webservice.@api_key;
				model.playerId = prefsXML.webservice.@player_id;
				model.fullscreenOnStartup = (prefsXML.webservice.@fullscreen_on_startup == "true");
				model.debug = (prefsXML.webservice.@debug == "true");
				model.showNewsticker = (prefsXML.webservice.@showNewsticker == "true");
				model.fileLogging = (prefsXML.webservice.@fileLogging == "true");
				model.traceLogging = (prefsXML.webservice.@traceLogging == "true");
				model.logLevel = prefsXML.webservice.@logLevel;
				
			//	model.log = "Exists "+ (prefsXML.webservice.@debug == "true") + " "+model.debug;
				
			}
			else
			{
				model.url = "none";
				model.apiKey = "none";
				model.playerId = "none";
				model.fullscreenOnStartup = false;
				model.debug = true;
				model.traceLogging = false;
				model.fileLogging = false;
				model.logLevel = 4;
				
				new UpdatePreferencesEvent(
						model.url,
						model.apiKey,
						model.playerId,
						model.fullscreenOnStartup,
						model.debug,
						model.showNewsticker,
						model.traceLogging,
						model.fileLogging,
						model.logLevel
					).dispatch();
			}
			
			executeNextCommand();
		}
	}
}